package com.ainiyar.springweb.controller.admin;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.pojo.admin.dto.AdminDto;
import com.ainiyar.springweb.pojo.admin.dto.PageDto;
import com.ainiyar.springweb.pojo.admin.dto.RegionDto;
import com.ainiyar.springweb.pojo.admin.vo.SelectVo;
import com.ainiyar.springweb.pojo.entity.Region;
import com.ainiyar.springweb.service.RegionService;
import com.ainiyar.springweb.util.ToolsUtil;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**************************************
 * File Name : RegionController
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/16  22:29
 * Desc :
 ***************************************/
@RestController
@RequestMapping("/region")
public class RegionController {
    @Resource
    private RegionService regionService;

    /**
     * 管理员列表
     *
     * @param pageDto   分页参数
     * @param regionDto 查询条件
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('region:list')")
    public ResultResponse<Object> list(PageDto pageDto, RegionDto regionDto) {
        return regionService.lists(pageDto, regionDto);
    }

    /**
     * 新增区域
     *
     * @param region 参数
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('region:add')")
    public ResultResponse<String> saveAdd(Region region) {
        if (ToolsUtil.isNullOrEmpty(region.getName())) {
            return ResultResponse.Fail("区域名称不能为空");
        }
        if (region.getPId() == null) {
            return ResultResponse.Fail("上级区域不能为空");
        }
        boolean add = regionService.saveAddEdit(region);
        if (add) {
            return ResultResponse.Success("新增区域成功");
        }
        return ResultResponse.Fail("新增区域失败");
    }

    /**
     * 修改区域
     *
     * @param region 参数
     */
    @PutMapping("/edit")
    @PreAuthorize("hasAuthority('region:edit')")
    public ResultResponse<String> saveEdit(Region region) {
        if (region.getPId() == null) {
            return ResultResponse.Fail("上级区域不能为空");
        }
        if (ToolsUtil.isNullOrEmpty(region.getName())) {
            return ResultResponse.Fail("区域名称不能为空");
        }
        boolean edit = regionService.saveAddEdit(region);
        if (edit) {
            return ResultResponse.Success("修改区域成功");
        }
        return ResultResponse.Fail("修改区域失败");
    }

    /**
     * 删除区域
     * @param id 区域id
     */
    @DeleteMapping("/del/{id}")
    @PreAuthorize("hasAuthority('region:del')")
    public ResultResponse<String> delRegion(@PathVariable Integer id) {
        if (regionService.hasRegionChildren(id)) {
            return ResultResponse.Fail("删除失败、当前区域存在子区域，请先删除子区域");
        }
        if (regionService.del(id)) {
            return ResultResponse.Success("删除区域成功");
        }
        return ResultResponse.Fail("删除区域失败");
    }
    /**
     * 为下拉框
     */
    @GetMapping("/get_tree")
    public ResultResponse<List<SelectVo>> getTree() {
        return ResultResponse.Result(regionService.getRegionForSelect());
    }

}

